﻿using BigEgg.Framework.Foundation;
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data.Objects.DataClasses;
using System.Linq;

namespace FMStudio.Domain.SQLite
{
    [MetadataType(typeof(IPayTypeCategory))]
    public partial class PayTypeCategory : IPayTypeCategory, IDataErrorInfo
    {
        #region Implement IDataErrorInfo Interface
        [NonSerialized]
        private readonly DataErrorInfoSupport dataErrorInfoSupport;

        string IDataErrorInfo.Error { get { return this.dataErrorInfoSupport.Error; } }

        string IDataErrorInfo.this[string memberName] { get { return this.dataErrorInfoSupport[memberName]; } }
        #endregion


        public PayTypeCategory()
        {
            this.dataErrorInfoSupport = new DataErrorInfoSupport(this);

            //  Default value
            this.Name = string.Empty;

            this.CurrencyReference.AssociationChanged += CurrencyReferenceAssociationChanged;
        }


        #region Implement IPayTypeCategory Interface
        ICurrency IPayTypeCategory.Currency
        {
            get { return this.Currency; }
            set { this.Currency = value as Currency; }
        }

        EntityCollection<IPayType> IPayTypeCategory.PayTypes 
        {
            get { return this.PayTypes.Cast<IPayType>() as EntityCollection<IPayType>; }
        }
        #endregion


        #region Private Methods
        private void CurrencyReferenceAssociationChanged(object sender, CollectionChangeEventArgs e)
        {
            // The navigation property doesn't support the PropertyChanged event. We have to raise it ourselves.
            OnPropertyChanged("Currency");
            this.CurrencyID = this.Currency.ID;
        }
        #endregion
    }
}
